# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import pymysql
class ScrapyEducationPipeline:
    def process_item(self, item, spider):
        DB_HOST = "127.0.0.1"
        DB_POST = 3306
        DB_USER = "root"
        DB_PASSWORD = "root"
        DB_NAME = "graduate-mysql"
        DB_CHART = "utf8"
        dbmy = pymysql.connect(
            host=DB_HOST,
            port=DB_POST,
            password=DB_PASSWORD,
            user=DB_USER,
            charset=DB_CHART,
            db=DB_NAME
        )
        # 操作模块
        cur = dbmy.cursor()
        sql = 'insert into news(title,author,content,category_id) values("{0}","{1}","{2}",6)'.format(item['title'], item['author'],item['content'])
        # # 执行sql语句
        print(item['title'])
        cur.execute(sql)
        # # 提交数据库
        dbmy.commit()
        # dbmy.close()
        return item
